package com.iadmin.web.system.controller;

import com.iadmin.web.framework.model.json.AjaxJson;
import com.iadmin.web.system.dao.TSUserDao;
import com.iadmin.web.system.entity.TSUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by Administrator on 2018/1/7.
 */
@Controller
public class LoginController {

    @Autowired
    private TSUserDao userDao;

    @RequestMapping(value = "/login", method = GET)
    public String index(Model model) {
        return "login";
    }

    @RequestMapping(params = "checkuser")
    @ResponseBody
    public AjaxJson checkUser(TSUser user, HttpServletRequest req){
//        HttpSession session = req.getSession();
//        int users = userService.getUsersOfThisRole("1122");
//        AjaxJson j = new AjaxJson();
//        //语言选择
//        if (req.getParameter("langCode")!=null) {
//            req.getSession().setAttribute("lang", req.getParameter("langCode"));
//        }
//        //验证码
//        String randCode = req.getParameter("randCode");
//        if (StringUtils.isEmpty(randCode)) {
//            j.setMsg(mutiLangService.getLang("common.enter.verifycode"));
//            j.setSuccess(false);
//        } else if (!randCode.equalsIgnoreCase(String.valueOf(session.getAttribute("randCode")))) {
//            j.setMsg(mutiLangService.getLang("common.verifycode.error"));
//            j.setSuccess(false);
//        } else {
//            //用户登录验证逻辑
//            TSUser u = userService.checkUserExits(user);
//            if (u == null) {
//                j.setMsg(mutiLangService.getLang("common.username.or.password.error"));
//                j.setSuccess(false);
//                return j;
//            }
//            if (u != null && u.getStatus() != 0) {
//                // 处理用户有多个组织机构的情况，以弹出框的形式让用户选择
//                Map<String, Object> attrMap = new HashMap<String, Object>();
//                j.setAttributes(attrMap);
//
//                String orgId = req.getParameter("orgId");
//                if (oConvertUtils.isEmpty(orgId)) {
//                    // 没有传组织机构参数，则获取当前用户的组织机构
//                    Long orgNum = systemService.getCountForJdbc("select count(1) from t_s_user_org where user_id = '" + u.getId() + "'");
//                    if (orgNum > 1) {
//                        attrMap.put("orgNum", orgNum);
//                        attrMap.put("user", u);
//                    } else {
//                        Map<String, Object> userOrgMap = systemService.findOneForJdbc("select org_id as orgId from t_s_user_org where user_id=?", u.getId());
//                        saveLoginSuccessInfo(req, u, (String) userOrgMap.get("orgId"));
//                    }
//                } else {
//                    attrMap.put("orgNum", 1);
//                    saveLoginSuccessInfo(req, u, orgId);
//                }
//            } else {
//
//                j.setMsg(mutiLangService.getLang("common.lock.user"));
//
//                j.setSuccess(false);
//            }
//        }
//        return j;
        return null;
    }
}
